module.exports = (sequelize, DataTypes) => {
  const User = sequelize.define('User', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    username: {
      type: DataTypes.STRING(50),
      allowNull: false,
      unique: true,
      validate: {
        len: [3, 50]
      }
    },
    password: {
      type: DataTypes.STRING,
      allowNull: true // 微信登录可能没有密码
    },
    avatar: {
      type: DataTypes.STRING,
      allowNull: true
    },
    email: {
      type: DataTypes.STRING,
      allowNull: true,
      unique: true,
      validate: {
        isEmail: true
      }
    },
    openId: {
      type: DataTypes.STRING,
      allowNull: true,
      unique: true
    },
    unionId: {
      type: DataTypes.STRING,
      allowNull: true,
      unique: true
    },
    lastLoginAt: {
      type: DataTypes.DATE,
      allowNull: true
    },
    totalGames: {
      type: DataTypes.INTEGER,
      defaultValue: 0
    },
    completedGames: {
      type: DataTypes.INTEGER,
      defaultValue: 0
    },
    bestRecords: {
      type: DataTypes.JSON,
      defaultValue: {
        easy: null,
        medium: null,
        hard: null
      }
    }
  }, {
    timestamps: true,
    paranoid: true // 软删除
  });

  return User;
};
